#include <QMouseEvent>
#include <QHeaderView>
#include <QFileDialog>
#include "core/order/ordermanager.h"
#include "orderstablewidget.h"

OrdersTableWidget::OrdersTableWidget(ORDERSTYPE type, QWidget *parent)
: QTableWidget(parent)
, m_type(type)
{
    m_tableMenu = new QMenu("Orders Management", this);
    if (type == ORDERSCURR) {
        m_closeOrderAction = new QAction("Close Order", this);
        m_closeAllOrdersAction = new QAction("Close All Order", this);
        m_tableMenu->addAction(m_closeOrderAction);
        m_tableMenu->addAction(m_closeAllOrdersAction);
    }
    else if (m_type == ORDERSHIST) {
        m_clearHistOrderAction = new QAction("Clear All History Orders", this);
        m_tableMenu->addAction(m_clearHistOrderAction);
    }
}

void OrdersTableWidget::init(const QStringList &strList)
{
    setColumnCount(strList.size());
    setHorizontalHeaderLabels(strList);
    setEditTriggers(QAbstractItemView::NoEditTriggers);
    setSelectionBehavior(QAbstractItemView::SelectRows);
    setSelectionMode(QAbstractItemView::SingleSelection);
    verticalHeader()->hide();
    setSortingEnabled(true);
    setContextMenuPolicy(Qt::CustomContextMenu);
    horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
    clearContents();
    if (m_type == ORDERSCURR) {
        connect(m_closeOrderAction, SIGNAL(triggered()), this, SLOT(slotCloseOrder()));
        connect(m_closeAllOrdersAction, SIGNAL(triggered()), this, SLOT(slotCloseAllOrders()));
    }
    else if (m_type == ORDERSHIST) {
        connect(m_clearHistOrderAction, SIGNAL(triggered()), this, SLOT(slotClearHistOrders()));
    }
}

void OrdersTableWidget::keyPressEvent(QKeyEvent *event)
{
    int keyVal = event->key();
    switch (keyVal) {
        case Qt::Key_O:
            OrderManager *orderManager = OrderManager::inst();
            QString strFile = QFileDialog::getOpenFileName(this, tr("Open File"), "Orders");
            orderManager->loadOrders(strFile.toLatin1().data());
            break;
    }
    QWidget::keyPressEvent(event);
}

void OrdersTableWidget::mousePressEvent(QMouseEvent *event)
{
    QTableWidget::mousePressEvent(event);
    event->accept();
    int w = event->globalX();
    int h = event->globalY();
    if (event->button() == Qt::RightButton) {
        m_tableMenu->popup(QPoint(w, h));
    }
}

void OrdersTableWidget::slotCloseOrder()
{
    int row = this->currentRow();
    if (0 <= row && row < rowCount()) {
        QTableWidgetItem *pItem = item(row, 0);
        if (pItem == 0)
            return;
        QString str = pItem->text();
        int orderNo = str.toInt();
        OrderManager *orderManager = OrderManager::inst();
        orderManager->closeOrder(orderNo);
    }
}

void OrdersTableWidget::slotCloseAllOrders(){
    OrderManager *orderManager = OrderManager::inst();
    orderManager->closeCurrOrders();
}

void OrdersTableWidget::slotClearHistOrders()
{
    OrderManager *orderManager = OrderManager::inst();
    orderManager->clearHistOrders();
}